import 'package:data/response/banner_response.dart';
import 'package:dio/dio.dart';
import 'package:util/result/catching.dart';
import 'package:util/result/result_extension.dart';

abstract class BannerRepository {
  Future<List<BannerResponse>> fetchBanners();
}

class BannerRepositoryImpl implements BannerRepository {
  final Dio _dio;

  const BannerRepositoryImpl({required Dio dio}) : _dio = dio;

  @override
  Future<List<BannerResponse>> fetchBanners() async {
    final apiResult = await runCatchingAsync(() async {
      final url = 'https://www.wanandroid.com/banner/json';
      return ((await _dio.get(url)).data['data'] as List<dynamic>)
          .map((json) => BannerResponse.fromJson(json))
          .toList();
    });
    return apiResult.getOrNull() ?? [];
  }
}
